package com.abigdreamer.im.client.module.talk.file;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Map;

import javax.swing.ImageIcon;

import com.abigdreamer.im.client.resourse.ResourcesManagement;
import com.abigdreamer.im.client.util.EIMLogger;
import com.abigdreamer.im.message.FileResponseMessage;


public class SendGetFileProgressPanle extends javax.swing.JPanel {
	
	private String fileAbsolutePath;
	private int num;
	private  ActionListener taskPerformer;
	private javax.swing.Timer timer=null;
	private static final int filenameLength=8;   //文件的名字最大长度
	public static final int timerTime=100;      //计数器的时间间隔
	private long time;
	
	public static Map<String, SendGetFileProgressPanle> progressBarMap=new HashMap<String, SendGetFileProgressPanle>();
	
    public SendGetFileProgressPanle(String flg,FileResponseMessage fileResponseMessage) {
    	super();
    	initComponents(fileResponseMessage);
    	if("send".equals(flg)){
    		progressBarMap.put(fileResponseMessage.getFileAbsolutePath(), this);
    	}else if("get".equals(flg)){
    		progressBarMap.put(fileResponseMessage.getSaveFilePath(), this);
    	}
    	this.setFileAbsolutePath(fileResponseMessage.getFileAbsolutePath()+fileResponseMessage.getSrcQQ());
    }
    
    private void initComponents(FileResponseMessage fileResponseMessage) {
    	
    	String fileName=fileResponseMessage.getFileName();
    	String fileType=getFileType(fileName).toLowerCase();
    	
    	String fileSgortName="";
    	int fileNameLength=fileName.length()-(fileType.length()+1);
    	
    	if(fileNameLength>filenameLength){
    		fileSgortName=fileName.substring(0, filenameLength)+".. ."+fileType;
    	}else{
    		fileSgortName=fileName;
    	}
    	
    	String fileTypeImage=getFileTypeImageName(fileType);
    	
    	jL_FileName = new javax.swing.JLabel();
        jp_sendGetProgress = new javax.swing.JProgressBar();
     	jp_sendGetProgress.setBackground(Color.white);
		jp_sendGetProgress.setForeground(Color.black);
		
        time_JLable = new javax.swing.JLabel();
        time_JLable.setText("500KB/s"+"时间:"+"11ms/13ms");
        ResourcesManagement.getImageIcon("\\fileIconType\\"+fileTypeImage).getIconHeight();
		ImageIcon imageIcon=ResourcesManagement.getImageIcon("\\fileIconType\\"+fileTypeImage,32, 32);
        jL_FileName.setIcon(imageIcon);
        jL_FileName.setText(fileSgortName);
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jL_FileName, javax.swing.GroupLayout.DEFAULT_SIZE, 112, Short.MAX_VALUE)
                    .addComponent(jp_sendGetProgress, 0, 0, Short.MAX_VALUE)
                    .addComponent(time_JLable))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jL_FileName, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jp_sendGetProgress, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(time_JLable)
                .addContainerGap())
        );
         
         
         taskPerformer = new ActionListener() {
             public void actionPerformed(ActionEvent evt) {
            	 EIMLogger.INFO("panle 传输百分比为"+num);
            	 changeProcessgarValue(num);
            	 if(num==100){
            		 time_JLable.setText("传完,均速:765KB/S");
            	 }
             }
         };
         timer= new javax.swing.Timer(timerTime, taskPerformer);
         timer.start();//每隔1秒触发一次 taskPerformer
    }
    
    public void changeProcessgarValue(int num){
    	jp_sendGetProgress.setValue(num);
    }
    
    
    public static  String getFileType(String fileName){
    	String fileType=null;
    	int start=fileName.indexOf('.');
    	fileType=fileName.substring(start+1);
    	return fileType;
    }
    
    public static  String getFileTypeImageName(String fileType){
    	String fileTypeImageName=null;
    	return fileTypeImageName;
    }
    
    
    // 变量声明 - 不进行修改
    private javax.swing.JLabel jL_FileName;
    private javax.swing.JProgressBar jp_sendGetProgress;
    private javax.swing.JLabel time_JLable;
    // 变量声明结束

	public String getFileAbsolutePath() {
		return fileAbsolutePath;
	}
	public void setFileAbsolutePath(String fileAbsolutePath) {
		this.fileAbsolutePath = fileAbsolutePath;
	}
	public int getNum() {
		return num;
	}
	public void setNum(int num) {
		this.num = num;
	}
	public javax.swing.Timer getTimer() {
		return timer;
	}
	public void setTimer(javax.swing.Timer timer) {
		this.timer = timer;
	}
	public long getTime() {
		return time;
	}
	public void setTime(long time) {
		this.time = time;
	}

}

